package Sort;

import java.util.Arrays;
import java.util.Random;

import static Sort.Sort.*;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 姚东名
 * Date: 2024-09-22
 * Time: 9:20
 */
public class Test {
    public static void orderArray(int[] array) {
        for (int i = 0; i < array.length; i++) {
            //数组的排序为逆序
            array[i] = array.length - 1 - i;
        }
    }
    public static void orderArrayNor(int[] array) {
        //采用生成随机数的方法
        Random random = new Random();
        for (int i = 0; i < array.length; i++) {
            array[i] = random.nextInt(10_0000);
        }
    }

    public static void testInsert(int[] array) {
        array = Arrays.copyOf(array, array.length);
        long startTime = System.currentTimeMillis();
        insetSort(array);
        long endTime = System.currentTimeMillis();
        System.out.println("直接插入排序耗时：" + (endTime - startTime));
    }
    public static void testShell(int[] array) {
        array = Arrays.copyOf(array, array.length);
        long startTime = System.currentTimeMillis();
        shellSort(array);
        long endTime = System.currentTimeMillis();
        System.out.println("希尔排序耗时：" + (endTime - startTime));
    }
    public static void testSelect(int[] array) {
        array = Arrays.copyOf(array, array.length);
        long startTime = System.currentTimeMillis();
        selectSort(array);
        long endTime = System.currentTimeMillis();
        System.out.println("选择排序耗时：" + (endTime - startTime));
    }
    public static void testHeap(int[] array) {
        array = Arrays.copyOf(array, array.length);
        long startTime = System.currentTimeMillis();
        HeapSort(array);
        long endTime = System.currentTimeMillis();
        System.out.println("堆排序耗时：" + (endTime - startTime));
    }
    public static void testBubble(int[] array) {
        array = Arrays.copyOf(array, array.length);
        long startTime = System.currentTimeMillis();
        bubbleSort(array);
        long endTime = System.currentTimeMillis();
        System.out.println("冒泡排序耗时：" + (endTime - startTime));
    }
    public static void testQuick(int[] array) {
        array = Arrays.copyOf(array, array.length);
        long startTime = System.currentTimeMillis();
        quickSort(array);
        long endTime = System.currentTimeMillis();
        System.out.println("快速排序耗时：" + (endTime - startTime));
    }
    public static void testMerge(int[] array) {
        array = Arrays.copyOf(array, array.length);
        long startTime = System.currentTimeMillis();
        mergeSort(array);
        long endTime = System.currentTimeMillis();
        System.out.println("归并排序耗时：" + (endTime - startTime));
    }

    public static void main(String[] args) {
        testSimple();// 测试是否排序成功
        //testOther();// 测排序所需要的时间
    }
    public static void testSimple() {
        int[] array = {9, 1, 2, 5, 7, 4, 8, 6, 3, 5};
        System.out.println("排序前：" + Arrays.toString(array));
        //insetSort(array);
        //shellSort(array);
        //selectSort(array);
        //HeapSort(array);
        //bubbleSort(array);
        //quickSort(array);
        mergeSort(array);
        System.out.println("排序后：" + Arrays.toString(array));
    }
    public static void testOther() {
        int[] array = new int[10_0000];
        //生成一个范围在0~10万的随机数的数组
        orderArrayNor(array);

        //测试各个排序方法的时间复杂度：
        //testInsert(array);
        testShell(array);
        //testSelect(array);
        testHeap(array);
        //testBubble(array);
        testQuick(array);
        testMerge(array);
    }
}
